package org.cnsoft.player.service;

import javax.annotation.Resource;

import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.beans.user.LoginerBean;
import org.cnsoft.framework.beans.user.UserBean;
import org.cnsoft.framework.support.MyTokenCommonSupport;
import org.cnsoft.framework.utils.EmptyHelper;
import org.cnsoft.player.ISPlayerCheckService;
import org.cnsoft.player.ISPlayerLoginService;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.cnsoft.persistent.player.U101010PlayerInfo.U101010PlayerInfoDao;

/**
 * 用户登录
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 * @see ApiManagerLoginController
 */
@Service("ISPlayerLoginService") // 替换系统登录接口
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class PlayerLoginService extends MyTokenCommonSupport implements ISPlayerLoginService {
	// setLoginSource 登录来源-10微信公众号11手机浏览器12苹果手机13安卓手机20APP应用30电脑网站
	// setLoginType 登录方式-0手机号1账号2微信3支付宝99中台
	// setRoolType 用户类型（2新注册用户1注册用户0游客）
	/**
	 * 账号密码登录
	 */
	public RESTResultBean<UserBean> doDefaultLogin(LoginerBean loginer) throws Exception {
		loginer.setLoginSource(ELEVEN);
		loginer.setLoginType(ONE);

		return doPlayerLogin(myPlayerCheckService.checkUser(loginer,true));
	}

	/**
	 * 自动登录
	 * 
	 * @deprecated
	 */
	public RESTResultBean<UserBean> doAutoLogin(String token, String unionId) throws Exception {
		// 开始自动登录
		LoginerBean loginer = new LoginerBean();
		loginer.setToken(token);
		loginer.setLoginSource("20");
		loginer.setLoginType("99");

		return doPlayerLogin(myPlayerCheckService.checkUser(loginer,true));
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////
	@Resource
	protected ISPlayerCheckService myPlayerCheckService;//用户账号检查
	@Resource
	protected U101010PlayerInfoDao daoU101010PlayerInfo;//用户信息

	/**
	 * 账号密码登录
	 */
	public RESTResultBean<UserBean> doPlayerLogin(UserBean curUser) throws Exception {

		RESTResultBean<UserBean> result = new RESTResultBean<UserBean>();
		/*
		 * if (EmptyHelper.isEmpty(curUser)) { TokenBusinessSupport tokenBean = TokenBusinessSupport.defaultToken(request, myCacheService); // 处理TOKEN result.setToken(tokenBean.getToken()); result.setData(tokenBean.currentUser()); return result; }
		 */

		String account = curUser.getAccount();
		String passWord = curUser.getPassWord();
		String uuid = curUser.getUuid();
		if (EmptyHelper.isEmpty(account)) {
			result.setCode(100);
			result.setMsg("手机号码不能为空!");

		} else if (EmptyHelper.isEmpty(passWord)) {
			result.setCode(101);
			result.setMsg("验证码不能为空!");

		} else if (EmptyHelper.isEmpty(uuid)) {
			result.setCode(102);
			result.setMsg("UUID不能为空!");

		} else {
			if (!super.checkPhoneNumSecurityCode(account, passWord, account)) {
				result.setCode(103);
				result.setMsg("验证不通过!");
			} else {

				// P611010PlayerAuthDBO player = new P611010PlayerAuthDBO();
				// player.setId(curUser.getUserId());
				// player = daoP611010PlayerAuth.doRead(player);
				// /*
				// * if (EmptyHelper.isEmpty(player)) { TokenBusinessSupport tokenBean = TokenBusinessSupport.defaultToken(request, myCacheService); // 处理TOKEN result.setToken(tokenBean.getToken()); result.setData(tokenBean.currentUser()); return result; }
				// */
				//
				// {
				// // 保存用户信息
				// curUser.setId(player.getId());
				// // curUser.setUserHeadUrl(player.getHeadImgUrl());
				// curUser.setUserPhone(player.getPhoneNumber());
				// // curUser.setUserNameCN(player.getNickName());
				// curUser.setAuthState(player.getStatus());
				// curUser.setPurseState(player.getWalletStatus());
				// // 是否新用户
				// curUser.setRoolType(curUser.getRoolType());
				// // 保存系统类别
				// curUser.setHdp(SYSTEM);
				// }
				// // 制作Token
				// {
				// TokenBusinessSupport tokenBean = TokenBusinessSupport.build(request, curUser);
				// tokenBean.setMyCacheService(myCacheService);// 缓存中心缓存用户对象
				// tokenBean.setCurUser(curUser);
				// tokenBean.setWaitTimeSecond(1800);
				// tokenBean.saveToken(request);// 处理TOKEN
				//
				// result.setData(tokenBean);
				// result.setToken(tokenBean.getToken());
				// }
			}
		}

		return result;
	}

}
